import uuid
import time
import json
import urllib.parse
import requests
import hashlib
from core.wchat.wx_base import mc

# 我
appid = 'bebd3ab32baa4bc081a62fd491632896'
secret = "fb53e0845ce64f9c921e2aa361ebd12d"
nonceStr = uuid.uuid4().hex[:16]
timestamp = str(int(time.time()))


# 示例
# appid = 'd43714e0246a435e87037f80495d2c6d'
# secret = "22f781d6d85947618c93a5a75e3a51a1"
# timestamp = "1608606287"
# nonceStr = "ty5lI76BJxVjAlSk"

class FlashPayCore:
    def get_backend_token(self):
        key = "flash_backend_token"
        flash_backend_token = mc.get(key)
        if flash_backend_token:
            return flash_backend_token
        else:
            data = self.request_backend_token()
            flash_backend_token = data.get("backendToken")
            expires_in = data.get("expiresIn") - 60
            mc.set(key, flash_backend_token, expires_in)
            return flash_backend_token

    def request_backend_token(self, retry=1):
        if retry < 3:
            url = "https://open.95516.com/open/access/1.0/backendToken"
            base_data = {
                "appId": appid,
                "nonceStr": nonceStr,
                "secret": secret,
                "timestamp": timestamp,
            }
            message = urllib.parse.urlencode(base_data)
            message = message.encode('utf-8')
            hash_object = hashlib.sha256(message)
            hex_dig = hash_object.hexdigest()
            base_data["signature"] = hex_dig
            base_data.pop("secret")
            re = requests.post(url, json=base_data)
            response = json.loads(re.text)
            if response.get("resp") == "00":
                return response.get("params")
            else:
                self.request_backend_token(retry=2)
        # {"resp":"00","msg":"成功","params":{"expiresIn":7200,"backendToken":"0981a358200600601SVJyjze"}}

    def get_access_token(self, code):
        flash_backend_token = self.get_backend_token()
        post_data = {
            "appId": appid,
            "backendToken": flash_backend_token,
            "code": code,
            "grantType": "authorization_code",
        }
        url = "https://open.95516.com/open/access/1.0/token"
        response = requests.post(url, json=post_data)
        response = json.loads(response.text)
        if response.get("resp") == "00":
            return response.get("params")
        # "params": {
        #     "accessToken": "******",
        #     "expiresIn": 3600,
        #     "refreshToken": "******",
        #     "openId": "******",
        #     "scope": "upapi_mobile",
        #     "unionId": "******"
        # }

    def get_user_mobile(self, open_id, access_token):
        flash_backend_token = self.get_backend_token()
        url = "https://open.95516.com/open/access/1.0/user.mobile"
        data = {
            "appId": appid,
            "openId": open_id,
            "accessToken": access_token,
            "backendToken": flash_backend_token
        }
        response = requests.post(url, json=data)
        response = json.loads(response.text)
        if response.get("resp") == "00":
            data = response.get("params")
            mobile = data.get("mobile")
            return mobile

        # re_data = {
        #     "resp": "00",
        #     "msg": "成功",
        #     "params": {
        #         "mobile": "******",
        #         "cntryPhoneCd": "******"
        #     }
        # }

if __name__ == '__main__':
    FlashPayCore().get_backend_token()
